package com.example.cc.client.autoconfigure;

import com.example.cc.client.initializer.QueueInitializer;
import com.example.cc.client.processor.RefreshValueAnnotationBeanPostProcessor;
import com.example.cc.client.properties.ConfigCenterProperties;
import com.example.cc.client.service.ClientRegisterService;
import com.example.cc.client.service.impl.ClientRegisterServiceImpl;
import com.example.cc.common.annotation.MyValue;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.client.RestTemplate;

/**
 * 负责核心配置，如服务注册、属性解析等
 */
@Configuration
@ConditionalOnClass(value = MyValue.class)
@EnableConfigurationProperties(ConfigCenterProperties.class)
@Import({RefreshValueAnnotationBeanPostProcessor.class, QueueInitializer.class})
@RequiredArgsConstructor
public class ConfigCoreAutoConfiguration {

    private final ConfigCenterProperties configCenterProperties;

    @Bean
    @ConditionalOnMissingBean(RestTemplate.class)
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Bean
    @ConditionalOnMissingBean(ClientRegisterService.class)
    public ClientRegisterService clientRegistrationService() {
        return new ClientRegisterServiceImpl(configCenterProperties);
    }

}
